package gss;

import java.math.BigInteger;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import org.apache.commons.math.fraction.BigFraction;

public class BigFractionUtils {

    /**
     * Tests if the fraction has no fractional part.
     * @return	true if the fraction represents an integer.
     */
    public static boolean isInteger(BigFraction f) {
	return f.reduce().getDenominatorAsInt() == 1;
    }

    /** min and max methods for BigFraction */
    /**
     * @return the larger of a and b.
     */
    public static BigFraction max(BigFraction a, BigFraction b){
	if(a.compareTo(b)==1){
		return a;
	}
	else{
		return b;
	}
    }

    /**
     * @return the smaller of a and b.
     */
    public static BigFraction min(BigFraction a, BigFraction b){
	if(a.compareTo(b)==-1){
		return a;
	}
	else{
		return b;
	}
    }

    /**
     * Computes ceiling function.
     *
     * @param	f	a BigFraction
     * @return	least integer greater than or equal to the fraction
     */
    public static BigInteger ceiling(BigFraction f) {
	BigDecimal d = f.bigDecimalValue(BigDecimal.ROUND_CEILING);
	return d.toBigIntegerExact();
    }

    /**
     * Computes floor function.
     *
     * @param	f	a BigFraction
     * @return	greatest integer less than or equal to the fraction
     */
    public static BigInteger floor(BigFraction f) {
	BigDecimal d = f.bigDecimalValue(BigDecimal.ROUND_FLOOR);
	return d.toBigIntegerExact();
    }
}
